package com.tul.exercise.base.java8.stream.map;

import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * Stream的map操作示例
 * @author tuliang
 */
public class MapTest {
    @Test
    @DisplayName("map()使用示例")
    public void test02() {
        List<String> strList = Stream.of("tuliang", "Zhang San", "Anna")
                .collect(Collectors.toList());
        // 字符串转大写
        strList.stream().map(str -> str.toUpperCase()).forEach(System.out::println);
        // 每个字符串加入后缀
        strList.stream().map(str ->  str + "_").forEach(System.out::println);

        // 根据每个字符串创建Person对象
        strList.stream().map(str -> createPerson(str)).forEach(System.out::println);
    }

    /**
     * 根据名称创建Person
     * @param name
     * @return Person
     */
    private Person createPerson(String name) {
        return new Person(name);
    }

    /**
     * Person类
     */
    private static class Person{
        private String name;

        public Person(String name) {
            this.name = name;
        }

        public String toString() {
            return ToStringBuilder.reflectionToString(this, ToStringStyle.SHORT_PREFIX_STYLE);
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }
    }

}
